<?php
/**
Handles the account settings for classes.

@author Michael Argarin
@author Chad Estioco
@version Second Semester, AY 2010-2011
*/

//After going through customize.php, user goes here. Handle the submitted form, if any.

$username = mysql_real_escape_string($_SESSION["username"]);
$accountid_qs = "SELECT * FROM account WHERE username='$username'";
$getaccountid_query = mysql_query($accountid_qs);
$accountid_array = mysql_fetch_array($getaccountid_query);
$accountid = $accountid_array["account_id"];

//The names of the fields in customize.php
//Note to self: accountSettingsC.php and customize.php are too tightly coupled for my liking.
$names = array("first_name", "middle_name", "last_name", "sex", "birthday",
               "contact_number", "country", "city_province", "municipality", "street");

//Note to self: Can I use this function for further abstraction?
function isAnythingSet(){
	global $names;
	$i = 0;
	$limit = count($names);
	
	while($i < $limit){
		if(isset($_POST[$names[$i]])){
			return true;
		}
		
		$i++;
	}
	
	return false;
}

function updateFields(){
	global $names, $accountid;
	$i = 0;
	$limit = count($names);
	
	while($i < $limit){
		if(isset($_POST[$names[$i]])){
			$update_query = mysql_query("UPDATE account SET " . $names[$i] . " = '" . $_POST[$names[$i]] . "' WHERE account_id = '$accountid'");
			if(!$update_query){
				die(mysql_error());
			}
		}
		
		$i++;
	}
}

$username = mysql_real_escape_string($_SESSION["username"]);
$userid_query = mysql_query("SELECT account_id FROM account WHERE username='$username'");
$userid_array = mysql_fetch_array($userid_query);
$userid = mysql_real_escape_string($userid_array["account_id"]);

if(isAnythingSet()){
	updateFields();
}
?>
<script language="javascript">
	function edit(courseid){
		window.open('editCourse.php?courseid=' + courseid, '_blank', 'width=500, height=500')
	}
</script>
<?php include("fetchData.php"); ?>
    <div class="form_section"><h1>Templates <input type="button" value="Create a New Template" onClick="win=window.open('addTemplate.php','Create New Template','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0');"></h1></div>
	<?php $result=fetchAccountTemplateJoin(); ?>
    <?php if(mysql_num_rows($result)==0){?>
        <span>You do not have any templates yet.</span>
    <?php } else {?>
        <table><ul class="pMirror">
            <?php while($data=mysql_fetch_array($result))
                 echo "<tr><td><li class='pMirror'>".$data['template_name']."</td><td><input type=\"button\" name=\"edit\" value=\"Edit\" onclick=\"win=window.open('editTemplate.php?template_id=".$data['template_id']."','Create New Template','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0')\"></td><td><input type=\"button\" name=\"delete\" value=\"Delete\" onclick=\"window.location='deleteTemplate.php?template_id=".$data['template_id']."'\"></li></td></tr>"
            ?>
        </ul></table>
    <?php }?>
	
	<div class="form_section"><h1>Grading Schemes
	<input type="button" value="Create a New Scheme" onclick="window.open('newscheme.php', '', 'width=500, height=500')" /></h1></div>
	<?php
		$brackets_query = mysql_query("SELECT * FROM scheme WHERE account_id = '$userid'");
		$i = 0;
		$brackets_limit = mysql_num_rows($brackets_query);
		
		echo "<ul class='pMirror'>\n";
		while($i < $brackets_limit){
			mysql_data_seek($brackets_query, $i);
			$bracket = mysql_fetch_array($brackets_query);
			echo "<li>" . $bracket["scheme_name"] . "</li>\n";
			$i++;
		}
		echo "</ul>";
	?>
	
	<div class="form_section"><h1>Courses taught <input type="button" value="Open a course" onClick="window.open('addCourse.php', '', 'width=500, height=500')" /></h1></div>
	<ul class="pMirror">
	<?php
		$coursestaught_query = mysql_query("SELECT course_name, course.school_id, course_id
		                                   FROM course WHERE course.account_id = '$userid'");
		$coursestaught_limit = mysql_num_rows($coursestaught_query);
		$i = 0;
		
		while($i < $coursestaught_limit){
			mysql_data_seek($coursestaught_query, $i);
			$course_array = mysql_fetch_array($coursestaught_query);
			$courseid = $course_array["course_id"];
			
			$school_query = mysql_query("SELECT school_name, school_id FROM school WHERE school_id = '" . $course_array["school_id"] . "'");
			if($school_query){
				$school_array = mysql_fetch_array($school_query);
				$schoolid = $school_array["school_id"];
				echo "<li>" . $school_array["school_name"] . " - " . $course_array["course_name"] . "<input type='button' value='Edit course' onclick='edit($courseid)'></li>\n";
			} else{
				echo "<li>" . $course_array["course_name"] . "<input type='button' value='Edit course' onclick='edit($courseid)'></li>\n";
			}
			$i++;
		}
	?>
	</ul>
	
	<div class="form_section"><h1>Add a Student to a Class <input type="button" value="Add class" onClick="window.open('new_class.php', '', 'width=500, height=500')"/></h1></div>
                <?php $result=fetchAccountClassJoin(); ?>
                <?php if(mysql_num_rows($result)==0){?>
                    <span>You do not have any class yet.</span>
                <?php } else {?>
                    <table><ul>
                    <?php while($data=mysql_fetch_array($result))
                        echo "<tr><td><li class='pMirror'>". $data["course_name"] . " - " . $data['class_name'] . "</td><td><input type=\"button\" name=\"edit\" value=\"Add Student\" onclick=\"win=window.open('addStudent.php?class_id=" . $data['class_id'] . "' ,'Add a Student','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,height=300,width=700')\"></td></li></tr>"
                    ?>
                    </ul></table>
                <?php }?>
<?php mysql_close();?>